package com.waterworld.watch.location.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopeer.itemtouchhelperextension.ItemTouchHelperExtension;
import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.db.TableConfig;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.location.adapter.ElectronicFenceAdapter;
import com.waterworld.watch.location.adapter.ItemTouchHelperCallBack;
import com.waterworld.watch.common.bean.ElectronicFenceBean;
import com.waterworld.watch.location.event.ElectronicFenceEvent;
import com.waterworld.watch.location.interfaces.ILocationManager;
import com.waterworld.watch.location.interfaces.onElectronicFenceClickListener;
import com.waterworld.watch.location.manager.LocationManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ElectronicFenceActivity extends BaseActivity {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.header_add)
    ImageView header_add;

    @BindView(R.id.rv_electronic_fence)
    RecyclerView rv_electronic_fence;

    private List<ElectronicFenceBean> mList;
    private ElectronicFenceAdapter mAdapter;
    private ILocationManager iLocationManager = LocationManager.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_electronic_fence);
        ButterKnife.bind(this);
        initView();
        EventBus.getDefault().register(this);
    }

    private void initView(){
        setViewSize(header_parent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this)/10);
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);
        header_add.setVisibility(View.VISIBLE);
        header_title.setText(getString(R.string.electronic_fence));

        mList = new ArrayList<>();
        if (iLocationManager != null) {
            iLocationManager.getSecurityFence(1, 100, new BaseObserverListener<BaseResultBean<ElectronicFenceBean[]>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(BaseResultBean<ElectronicFenceBean[]> baseResultBean) {
                    ElectronicFenceBean[] beans = baseResultBean.getData();
                    Collections.addAll(mList, beans);

                    mAdapter = new ElectronicFenceAdapter(ElectronicFenceActivity.this,mList);
                    rv_electronic_fence.setLayoutManager(new LinearLayoutManager(ElectronicFenceActivity.this));
                    rv_electronic_fence.setAdapter(mAdapter);

                    ItemTouchHelperExtension.Callback callback = new ItemTouchHelperCallBack();
                    ItemTouchHelperExtension itemTouchHelperExtension = new ItemTouchHelperExtension(callback);
                    itemTouchHelperExtension.attachToRecyclerView(rv_electronic_fence);

                    mAdapter.setOnElectronicFenceClickListener(new onElectronicFenceClickListener() {
                        @Override
                        public void onClick(View view, int position) {
                        }
                    });
                }
            });
        }

    }

    @OnClick(R.id.header_back)
    void onBack(){
        finish();
    }

    @OnClick(R.id.header_add)
    void onAdd(){
        startActivity(new Intent(this,AddElectronicFenceActivity.class).putExtra("position",mList.size()));
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void editElectronicFence(ElectronicFenceEvent event) {
        if (event.isNew()) {
            //新增围栏
            mList.add(event.getBean());
        } else {
            //修改围栏
            mList.set(event.getPosition(),event.getBean());
        }
        mAdapter.notifyDataSetChanged();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
